package com.ruoyi.store.service.impl;


import com.ruoyi.store.domain.StuQuesAnwser;
import com.ruoyi.store.domain.StuQuestion;
import com.ruoyi.store.mapper.StuQuesAnwMapper;
import com.ruoyi.store.mapper.StuQuestionMapper;
import com.ruoyi.store.mapper.StuQuestionStoreMapper;
import com.ruoyi.store.service.IStuQuestionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StuQuestionImpl implements IStuQuestionService {
    @Resource
    private StuQuestionMapper stuQuestionMapper;
    @Resource
    private StuQuesAnwMapper stuQuesAnwMapper;
    @Resource
    private StuQuestionStoreMapper stuQuestionStoreMapper;

    @Override
    public List<StuQuestion> selectStuQuestionList(StuQuestion stuQuestion) {
        return stuQuestionMapper.selectStuQuestionList(stuQuestion);
    }

    @Override
    public List<Long> selectStuStoresListByQuesId(Long quesId) {
        return stuQuestionMapper.selectStuStoresListByQuesId(quesId);
    }

    @Override
    public StuQuestion selectQuesById(Long quesId) {
        return stuQuestionMapper.selectQuesById(quesId);
    }

    @Override
    @Transactional
    public int insertStuQuestion(StuQuestion stuQuestion) {
        //将题目相关信息插入题干表
        stuQuestionMapper.insertStuQuestion(stuQuestion);
        //将答案封装插入答案表
        StuQuesAnwser stuQuesAnwser = setStuAnswer(stuQuestion);
        //插入答案表
        stuQuesAnwMapper.insertStuQuesAnw(stuQuesAnwser);
        //刷新题库表题目数量
        return  stuQuestionStoreMapper.refreshStat(stuQuestion.getStoreId().toString());
    }

    @Override
    @Transactional
    public int updateStuQuestion(StuQuestion stuQuestion) {
        Long quesId = stuQuestion.getQuesId();
        StuQuestion oldstuQuestion = stuQuestionMapper.selectQuesById(quesId);
        Long storeId = oldstuQuestion.getStoreId();
        //封装答案表相关信息
        StuQuesAnwser stuQuesAnwser = setStuAnswer(stuQuestion);

        stuQuesAnwMapper.updateAnw(stuQuesAnwser);
        stuQuestionMapper.updateStuQuestion(stuQuestion);
        //对题库表数量进行两次刷新
        stuQuestionStoreMapper.refreshStat(storeId.toString());
        //刷新题库表题目数量
        return stuQuestionStoreMapper.refreshStat(stuQuestion.getStoreId().toString());
    }

    /**
     * 封装答案表相关信息
     * @param stuQuestion
     * @return
     */
    public StuQuesAnwser setStuAnswer(StuQuestion stuQuestion){
        StuQuesAnwser stuQuesAnwser = new StuQuesAnwser();
        stuQuesAnwser.setSelContA(stuQuestion.getSelContA());
        stuQuesAnwser.setSelContB(stuQuestion.getSelContB());
        stuQuesAnwser.setSelContC(stuQuestion.getSelContC());
        stuQuesAnwser.setSelContD(stuQuestion.getSelContD());
        stuQuesAnwser.setAnwser(stuQuestion.getAnswer());
        stuQuesAnwser.setQuesId(stuQuestion.getQuesId());
        String[] manyAnswer = stuQuestion.getManyAnswer();
        String manyAnswers = "";
        if (manyAnswer != null){
            for (int i=0;i<manyAnswer.length;i++){
                manyAnswers += manyAnswer[i];
            }
            stuQuesAnwser.setManyAnswer(manyAnswers);
        }
        return stuQuesAnwser;
    }

    @Override
    @Transactional
    public int delStuQuesByIds(Long[] quesIds) {
        stuQuesAnwMapper.delAnwserById(quesIds);
        StuQuestion stuQuestion = stuQuestionMapper.selectQuesById(quesIds[0]);
        stuQuestionMapper.delStuQuesByIds(quesIds);
        return  stuQuestionStoreMapper.refreshStat(stuQuestion.getStoreId().toString());
    }


}
